package com.enjoyor.soa.traffic.server.efoms.dubbo.listener;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.enjoyor.soa.traffic.api.dto.collectms.DeviceStatusDto;
import com.enjoyor.soa.traffic.api.dto.idcs.BayonetStatusDto;
import com.enjoyor.soa.traffic.api.dto.idcs.VehiclePassDto;
import com.enjoyor.soa.traffic.api.dto.ovms.DevStatusHisDto;
import com.enjoyor.soa.traffic.api.dto.vms.LedStatusDto;
import com.enjoyor.soa.traffic.api.videoms.dto.VideoStatusDto;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceDetailInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.server.efoms.common.Global;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboBayonetServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboCollectCallbackServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboFxcDeviceServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboLedStatusInfoServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboVideoInfoServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.manage.ManageDevWranHandleService;
import com.enjoyor.soa.traffic.server.efoms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

/**
 *
 * 对IPPool内的所有的ip进行ping，获取结果
 * 通过监听机制，将结果放入事件体中
 * @ClassNameIPPingDevService
 * @Description TODO
 * @Author fzj
 * @Date 2019/3/1 9:41
 * @Version 1.0
 **/
@Component
public class ScheduledGetDeviceService {

	@Autowired
    private DubboBayonetServiceInvoke bayonetStatusInvoke;
	@Autowired
    private DubboCollectCallbackServiceInvoke collectStatusInvoke;
	@Autowired
    private DubboFxcDeviceServiceInvoke fxcStatusInvoke;
	@Autowired
    private DubboLedStatusInfoServiceInvoke ledStatusInvoke;
	@Autowired
    private DubboVideoInfoServiceInvoke videoStatusInvoke;
	@Autowired
    private ManageDevWranHandleService handleService;
	
	//设备在线状态
	String statusNaomalCode = EnumDicPublic.DEVICE_STATUS_ONLINE.getKey();
	String statusNaomalName = EnumDicPublic.DEVICE_STATUS_ONLINE.getValue();
	//设备离线状态
	String statusFailureCode = EnumDicPublic.DEVICE_STATUS_UNLINE.getKey();
	String statusFailureName = EnumDicPublic.DEVICE_STATUS_UNLINE.getValue();
	
	//默认操作人
	String personName = "状态同步";
	
	@Value("${device.status.led.issyn}")
    private String ledIssyn;
	@Value("${device.status.video.issyn}")
    private String videoIssyn;
	@Value("${device.status.bayonet.issyn}")
    private String bayonetIssyn;
	@Value("${device.status.collect.issyn}")
    private String collectIssyn;
	@Value("${device.status.fxc.issyn}")
    private String fxcIssyn;

    /**
     * 定时获取各子系统设备状态数据
     */
	//同步诱导屏状态
    @Scheduled(cron="0 */${device.status.syn.period} * * * ?")
    @SuppressWarnings("unchecked")
    public void getLedStatusScheduled(){
    	//if(!"1".equals(ledIssyn)) return;
        ResultPojo ledPojo = ledStatusInvoke.getLedStatusInfo();
        if(CheckPojoHelper.isValid(ledPojo)) {
        	List<LedStatusDto> ledStatusList = (List<LedStatusDto>)ledPojo.getResultList();
    		String typeCode = EnumDicPublic.INSCREEN.getKey();
    		String typeName = EnumDicPublic.INSCREEN.getValue();
    		/*List<WranTacticsInfo> wranTacticsList = manageWranTacticsService.selectWranTacticsList(
    				statusWranTypeCode, typeCode, null);*/
        	for(LedStatusDto ledStatusDto : ledStatusList) {
        		String devId = ledStatusDto.getLedId();
        		String key = typeCode + "-" + devId;
        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        		String devName = "";
        		if(null != deviceDetailInfo) {
        			devName = deviceDetailInfo.getDevName();
        		}else {
        			continue;
        		}
        		String linkStatusCode = "";
        		String linkStatusName = "";
        		if("0".equals(ledStatusDto.getStatus())) {
        			linkStatusCode = statusNaomalCode;
        			linkStatusName = statusNaomalName;
        		}else {
        			linkStatusCode = statusFailureCode;
        			linkStatusName = statusFailureName;
        		}
        		//子系统设备状态数据与本系统状态表、内存同步
        		handleService.devStatusHandle(typeCode, typeName, devId, devName, linkStatusCode, linkStatusName, personName);
        	    //根据报警策略生成状态异常报警数据
        		handleService.devStatusWranHandle(typeCode, typeName, devId, devName, linkStatusCode, personName);
        	}
        }
    }
    //同步视频监测状态
    @Scheduled(cron="0 */${device.status.syn.period} * * * ?")
    @SuppressWarnings("unchecked")
    public void getVideoStatusScheduled(){
    	if(!"1".equals(videoIssyn)) return;
        ResultPojo videoPojo = videoStatusInvoke.getVideoStatusInfo();
        if(CheckPojoHelper.isValid(videoPojo)) {
			List<VideoStatusDto> videoStatusList = (List<VideoStatusDto>)videoPojo.getResultList();
        	String typeCode = EnumDicPublic.VIDEO.getKey();
    		String typeName = EnumDicPublic.VIDEO.getValue();
    		/*List<WranTacticsInfo> wranTacticsList = manageWranTacticsService.selectWranTacticsList(
    				statusWranTypeCode, typeCode, null);*/
        	for(VideoStatusDto videoStatusDto : videoStatusList) {
        		String devId = videoStatusDto.getVideoid();
        		String key = typeCode + "-" + devId;
        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        		String devName = "";
        		if(null != deviceDetailInfo) {
        			devName = deviceDetailInfo.getDevName();
        		}else {
        			continue;
        		}
        		String linkStatusCode = "";
        		String linkStatusName = "";
        		if("1".equals(videoStatusDto.getStatus())) {
        			linkStatusCode = statusNaomalCode;
        			linkStatusName = statusNaomalName;
        		}else {
        			linkStatusCode = statusFailureCode;
        			linkStatusName = statusFailureName;
        		}
        		//子系统设备状态数据与本系统状态表、内存同步
        		handleService.devStatusHandle(typeCode, typeName, devId, devName, linkStatusCode, linkStatusName, personName);
        	    //根据报警策略生成状态异常报警数据
        		handleService.devStatusWranHandle(typeCode, typeName, devId, devName, linkStatusCode, personName);
        	}
        }
    }
    //同步卡口状态
    @Scheduled(cron="0 */${device.status.syn.period} * * * ?")
    @SuppressWarnings("unchecked")
    public void getBayonetStatusScheduled(){
    	//if(true) return;
    	//if(!"1".equals(bayonetIssyn)) return;
    	int sysNum = 0;
    	long startTime = new Date().getTime();
        ResultPojo bayonetPojo = bayonetStatusInvoke.getBayonetStatusInfo();
        if(CheckPojoHelper.isValid(bayonetPojo)) {
			List<BayonetStatusDto> bayonetStatusList = (List<BayonetStatusDto>)bayonetPojo.getResultList();
        	String typeCode = EnumDicPublic.FASTENER.getKey();
    		String typeName = EnumDicPublic.FASTENER.getValue();
        	for(BayonetStatusDto bayonetStatusDto : bayonetStatusList) {
        		
        		String devId = bayonetStatusDto.getBayonet_id();
        		String key = typeCode + "-" + devId;
        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        		String devName = "";
        		if(null != deviceDetailInfo) {
        			devName = deviceDetailInfo.getDevName();
        		}else {
        			continue;
        		}
        		
        		String receiveTime = bayonetStatusDto.getLast_receive_time();
        		String collectTime = bayonetStatusDto.getLast_pass_time();
        		String writeDbTime = bayonetStatusDto.getLast_record_time();
        		String picUrl = bayonetStatusDto.getLast_vehicle_pic();//最近过车图片；
        		if((null != receiveTime && receiveTime != "") || (null != collectTime && collectTime != "" 
        				&& null != writeDbTime && writeDbTime != "")) {
            		//根据策略生成数据报警
        			sysNum = sysNum + 1;
            		handleService.devDataWranHandle(typeCode, typeName, devId, devName, collectTime, 
            				receiveTime, writeDbTime,picUrl,"同步卡口状态");
        		}
        	}
        }
        long endTime = new Date().getTime();
        System.out.println("-----------------------------"+(endTime-startTime)+"--------------------------------");
        System.out.println("共匹配卡口数为：*******************"+sysNum+"************************************");
    }
    //同步非现场--电警
    @Scheduled(cron="0 */${device.status.syn.period} * * * ?")
    @SuppressWarnings("unchecked")
    public void getFxcStatusScheduled(){
    	if(!"1".equals(fxcIssyn)) return;
    	ResultPojo bayonetPojo = bayonetStatusInvoke.getBayonetStatusInfo();
        if(CheckPojoHelper.isValid(bayonetPojo)) {
			List<BayonetStatusDto> bayonetStatusList = (List<BayonetStatusDto>)bayonetPojo.getResultList();
        	String typeCode = EnumDicPublic.ELEPOLICE.getKey();
    		String typeName = EnumDicPublic.ELEPOLICE.getValue();
        	for(BayonetStatusDto bayonetStatusDto : bayonetStatusList) {
        		
        		String devId = bayonetStatusDto.getBayonet_id();
        		String key = typeCode + "-" + devId;
        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        		String devName = "";
        		if(null != deviceDetailInfo) {
        			devName = deviceDetailInfo.getDevName();
        		}else {
        			continue;
        		}
        		
        		String receiveTime = bayonetStatusDto.getLast_receive_time();
        		String collectTime = bayonetStatusDto.getLast_pass_time();
        		String writeDbTime = bayonetStatusDto.getLast_record_time();
        		String picUrl = bayonetStatusDto.getLast_vehicle_pic();//最近过车图片；
        		if((null != receiveTime && receiveTime != "") || (null != collectTime && collectTime != "" 
        				&& null != writeDbTime && writeDbTime != "")) {
            		//根据策略生成数据报警
            		handleService.devDataWranHandle(typeCode, typeName, devId, devName, collectTime, 
            				receiveTime, writeDbTime,picUrl, "同步电警状态");
        		}
        	}
        }
        /*ResultPojo fxcPojo = fxcStatusInvoke.getFxcStatusInfo(null);
        if(CheckPojoHelper.isValid(fxcPojo)) {
			List<DevStatusHisDto> fxcStatusList = (List<DevStatusHisDto>)fxcPojo.getResultList();
        	String typeCode = EnumDicPublic.ELEPOLICE.getKey();
    		String typeName = EnumDicPublic.ELEPOLICE.getValue();
        	for(DevStatusHisDto fxcStatusDto : fxcStatusList) {
        		String devId = fxcStatusDto.getDev_id();
        		String key = typeCode + "-" + devId;
        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        		String devName = "";
        		if(null != deviceDetailInfo) {
        			devName = deviceDetailInfo.getDevName();
        		}else {
        			continue;
        		}
        		String linkStatusCode = "";
        		String linkStatusName = "";
        		if("1".equals(fxcStatusDto.getStatus())) {
        			linkStatusCode = statusNaomalCode;
        			linkStatusName = statusNaomalName;
        		}else {
        			linkStatusCode = statusFailureCode;
        			linkStatusName = statusFailureName;
        		}
        		//子系统设备状态数据与本系统状态表、内存同步
        		//handleService.devStatusHandle(typeCode, typeName, devId, devName, linkStatusCode, linkStatusName, personName);
        	    //根据报警策略生成状态异常报警数据
        		//handleService.devStatusWranHandle(typeCode, typeName, devId, devName, linkStatusCode, personName);
        	}
        }*/
    }
    //同步采集设备状态
    @Scheduled(cron="0 */${device.status.syn.period} * * * ?")
    @SuppressWarnings("unchecked")
    public void getCollectStatusScheduled(){
    	if(!"1".equals(collectIssyn)) return;
    	ResultPojo collectPojo = collectStatusInvoke.getCollectStatusInfo();
        if(CheckPojoHelper.isValid(collectPojo)) {
			List<DeviceStatusDto> collectStatusList = (List<DeviceStatusDto>)collectPojo.getResultList();
        	for(DeviceStatusDto collectStatusDto : collectStatusList) {
        		String typeCode = "";
        		String typeName = "";
        		if("01".equals(collectStatusDto.getType())) {
        			typeCode = EnumDicPublic.WAVELET.getKey();
            		typeName = EnumDicPublic.WAVELET.getValue();
            		//wranTacticsList = wranTacticsListWave;
        		}else if("02".equals(collectStatusDto.getType())) {
        			typeCode = EnumDicPublic.MAGNETISM.getKey();
            		typeName = EnumDicPublic.MAGNETISM.getValue();
            		//wranTacticsList = wranTacticsListMagn;
        		}else if("03".equals(collectStatusDto.getType())) {
        			typeCode = EnumDicPublic.WINDING.getKey();
            		typeName = EnumDicPublic.WINDING.getValue();
            		//wranTacticsList = wranTacticsListWind;
        		}else if("04".equals(collectStatusDto.getType())) {
        			typeCode = EnumDicPublic.VIDEOCHECK.getKey();
            		typeName = EnumDicPublic.VIDEOCHECK.getValue();
            		//wranTacticsList = wranTacticsListWind;
        		}else{
        			continue;
        		}
        		String devId = collectStatusDto.getDevId();
        		String key = typeCode + "-" + devId;
        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        		String devName = "";
        		if(null != deviceDetailInfo) {
        			devName = deviceDetailInfo.getDevName();
        		}else {
        			continue;
        		}
        		/*String linkStatusCode = "";
        		String linkStatusName = "";
        		if("0".equals(collectStatusDto.getStatus())) {
        			linkStatusCode = statusNaomalCode;
        			linkStatusName = statusNaomalName;
        		}else {
        			linkStatusCode = statusFailureCode;
        			linkStatusName = statusFailureName;
        		}*/
        		//子系统设备状态数据与本系统状态表、内存同步
        		//handleService.devStatusHandle(typeCode, typeName, devId, devName, linkStatusCode, linkStatusName, personName);
        	    //根据报警策略生成状态异常报警数据
        		//handleService.devStatusWranHandle(typeCode, typeName, devId, devName, linkStatusCode, personName);
        		
        		String receiveTime = collectStatusDto.getReceiveTime();
        		String collectTime = collectStatusDto.getCollectTime();
        		String writeDbTime = collectStatusDto.getWriteDbTime();
        		if((null != receiveTime && receiveTime != "") || (null != collectTime && collectTime != "" 
        				&& null != writeDbTime && writeDbTime != "")) {
            		//根据策略生成数据报警
            		handleService.devDataWranHandle(typeCode, typeName, devId, devName, collectTime, 
            				receiveTime, writeDbTime,null, "同步采集设备状态");
        		}
        	}
        }
    }
    
    
    
    
}
